﻿@model CardPage

@{
    ViewBag.Title = "Card";
}

<h2>Card</h2>
@foreach (var script in Model.Scripts)
    {
    <script type="text/javascript">
    
        function @script.FunctionName
        {
            @Html.Raw(script.Value)
        }
    </script>
    }
@using (Html.BeginForm())
{
    <fieldset>
        <legend>Log in Form</legend>
        <ol>
            @foreach (var item in Model.Fields.Field)
            {
                if (item.DataType == "select")
                {
                    string[] optionSplit = item.Options.Split(',');
                <li>
                    <label for="@item.Name">@item.Label</label>
                    <select data-val="@item.IsRequired.ToString().ToLowerInvariant()" data-val-required="Required." id="@item.Name" name="@item.Name">
                        <option value=""></option>
                        @foreach (string option in optionSplit)
                        {
                            if (item.Value == option)
                            {
                            <option value="@option" selected>@option</option>
                            }
                            else
                            {
                            <option value="@option">@option</option>
                            }
                        }
                    </select>

                    <span class="field-validation-valid" data-valmsg-for="@item.Name" data-valmsg-replace="true"></span>
                </li>
                }
                else if (item.DataType == "table")
                {
                    string query = "SELECT [{0}],[{1}] FROM [{2}]";
                    query = string.Format(query, item.StoreField, item.DisplayField, item.TableLink);
                    System.Data.DataSet ds = DynamicForm.Manager.DataAccess.RunSelectStatement(query);
                <li>
                    <label for="@item.Name">@item.Label</label>
                    <select data-val="@item.IsRequired.ToString().ToLowerInvariant()" data-val-required="Required." id="@item.Name" name="@item.Name">
                        <option value=""></option>
                        @if (ds != null && ds.Tables.Count>0)
                        {
                            foreach (System.Data.DataRow row in ds.Tables[0].Rows)
                            {
                                if (item.Value == row[0].ToString())
                                {
                                <option value="@row[0].ToString()" selected>@row[1].ToString()</option>
                                }
                                else
                                {
                                <option value="@row[0].ToString()">@row[1].ToString()</option>
                                }
                            }
                        }
                    </select>

                    <span class="field-validation-valid" data-valmsg-for="@item.Name" data-valmsg-replace="true"></span>
                </li>
                }
                else if(item.DataType == "checkbox")
                {
                    string checkVal = "";
                    if (item.Value == "true")
                    {
                        checkVal = "checked = \"checked\"";
                    }
                <li>
                    <label for="@item.Name">@item.Label</label>
                    @if(item.Editable)
                    {
                        <input data-val="@item.IsRequired.ToString().ToLowerInvariant()" data-val-required="Required." id="@item.Name" name="@item.Name" type="checkbox" @checkVal/>
                    }
                    else
                    {
                        <input data-val="@item.IsRequired.ToString().ToLowerInvariant()" data-val-required="Required." id="@item.Name" name="@item.Name" disabled="disabled" type="checkbox" @checkVal/>                        
                    }
                    <span class="field-validation-valid" data-valmsg-for="@item.Name" data-valmsg-replace="true"></span>
                </li>
                }
                else
                {
                <li>
                    <label for="@item.Name">@item.Label</label>
                    @if(item.Editable)
                    {
                    <input data-val="@item.IsRequired.ToString().ToLowerInvariant()" data-val-required="Required." id="@item.Name" name="@item.Name" type="@item.DataType" value="@item.Value" />
                    }
                    else
                    {
                    <input data-val="@item.IsRequired.ToString().ToLowerInvariant()" data-val-required="Required." id="@item.Name" name="@item.Name" disabled="disabled" type="@item.DataType" value="@item.Value" />                        
                    }
                    <span class="field-validation-valid" data-valmsg-for="@item.Name" data-valmsg-replace="true"></span>
                </li>
                }
            }
        </ol>
        
        @if (Request.QueryString.AllKeys.Count() > 0)
        {
            <input type="submit" value="Update" />
        }
        else
        {
            <input type="submit" value="Create" onclick="document.getElementById('callfunc').value = 'FuncLib@CreateNewLead'" />
        }

        <input type="hidden" id="callfunc" name="callfunc" />
    </fieldset>
   
}


@foreach (var field in Model.Fields.Field)
{
    if (field.Scripts != null)
    {
        foreach (var script in field.Scripts)
        {
            <script type="text/javascript">
                $(function(){
                    $("#@field.Name").@script.Event
                    ( function() {
                        @if(!string.IsNullOrWhiteSpace(script.Value))
                        {
                            @Html.Raw(script.Value);
                        }
                        else
                        {
                            @Html.Raw(script.FunctionCall);
                        }

                    });
                });
            </script>
        }
    }
}